package reduce.fz.com.reduce.activity;

import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.Pic;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Society;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.SocietyEvent;
import reduce.fz.com.reduce.service.SocietyService;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.util.ScreenUtils;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.ToastUtil;
import reduce.fz.com.reduce.util.base.CropParam;
import reduce.fz.com.reduce.util.oss.Oss;
import reduce.fz.com.reduce.util.photo.PhotoSelect;
import reduce.fz.com.reduce.util.photo.PhotoTake;
import reduce.fz.com.reduce.util.photo.SelectParam;

public class SocietyCreateActivity extends BaseToolBarActivity implements View.OnClickListener {


    private RelativeLayout rlTakePhoto;
    private ImageView mTeamIcon;
    private EditText mTeamTitle;
    private EditText mTeamDesc;
    private PhotoSelect photoSelet;
    private Uri imageUri;
    private PhotoTake photoTake = null;
    private LoadingDialog dialog;
    private String iconurl;
    private TextView mComplete;
    private TextView mCircleGray;
    private SocietyService societyService = ServiceBuilder.builderSocietyService(null);


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_society_create);
        setToolbarTitle("新建社团");
        photoTake = PhotoTake.instance(SocietyCreateActivity.this);
        initWidget();
        // resetSize();

    }

    public void resetSize() {
        int margin = getResources().getDimensionPixelSize(R.dimen.dimen_60);
        int width = ScreenUtils.getScreenW() - getResources().getDimensionPixelSize(R.dimen.dimen_252) - getResources().getDimensionPixelSize(R.dimen.dimen_60);
        int height = (int) (width / 1);

        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(width, height);
        param.setMargins(margin, 0, margin, 0);
        rlTakePhoto.setLayoutParams(param);
        RelativeLayout.LayoutParams param1 = new RelativeLayout.LayoutParams(height, height);
        param1.addRule(RelativeLayout.CENTER_IN_PARENT);
        mCircleGray.setLayoutParams(param1);
    }

    @Override
    public void initWidget() {
        rlTakePhoto = (RelativeLayout) findViewById(R.id.rl_take_photo);
        mTeamIcon = (ImageView) findViewById(R.id.iv_team_icon);
        mTeamTitle = (EditText) findViewById(R.id.et_team_title);
        mTeamDesc = (EditText) findViewById(R.id.et_team_desc);
        mComplete = (TextView) findViewById(R.id.tv_complete);
        mCircleGray = (TextView) findViewById(R.id.tv_circle_gray);
        mComplete.setOnClickListener(this);
        rlTakePhoto.setOnClickListener(this);
        mTeamIcon.setOnClickListener(this);

    }


    public void selectPhoto() {
        SelectParam param = new SelectParam();
        param.selectMax = 1;
        photoSelet = PhotoSelect.instance(this, param);
        photoSelet.selectPhoto();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PhotoSelect.SELECT_PIC:
                ArrayList<String> paths = photoSelet.getSelectPaths(data);
                imageUri = Uri.fromFile(new File(paths.get(0)));
                CropParam param = new CropParam(10, 10, 800, 800, false);
                imageUri = photoTake.cropImageUri(imageUri, param);
                setTeamicon();
                break;
            case PhotoTake.CROP_PIC:
                setTeamicon();
                break;
        }
    }

    public void setTeamicon() {
        mTeamIcon.setVisibility(View.VISIBLE);
        mTeamIcon.setImageURI(imageUri);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_team_icon:
            case R.id.rl_take_photo:
                selectPhoto();
                break;
            case R.id.tv_complete:
                uploadTeamIcon();
                break;

            default:
                break;
        }

    }

    public void uploadTeamIcon() {
        if (!validate()) {
            ToastUtil.showMessage("请上传社团封片!");
            return;
        }
        dialog = LoadingDialog.create(this).setMsg("上传图片中!");
        dialog.show();
        Oss.instance().uploadPic(imageUri.getPath(), new Oss.OssUploadResponse() {

            @Override
            public void onSuccess(List<Pic> successPics, List<Pic> failedPics, List<Pic> all) {
                if (successPics != null && !successPics.isEmpty()) {
                    iconurl = successPics.get(0).url;
                    submit();
                } else {
                    ToastUtil.showMessage("上传图片异常");
                    return;
                }


            }

            @Override
            public void onPrgress(int complete, int total) {

            }
        });
    }

    public Boolean validate() {
        if (imageUri == null) {
            toastMsg("请选择一张社团封面!");
            return false;
        }
        if (TextUtils.isEmpty(mTeamTitle.getText().toString())) {
            toastMsg("请填写社团名称");
            return false;
        }
        if (TextUtils.isEmpty(mTeamDesc.getText().toString())) {
            toastMsg("请填写社团描述");
        }
        return true;

    }

    /**
     * 提交信息
     */
    public void submit() {
        if (dialog != null) dialog.setMsg("提交信息中!");
        final Society society = new Society();
        society.thumurl = iconurl;
        society.sname = mTeamTitle.getText().toString();
        society.description = mTeamDesc.getText().toString();
        dialog.dismiss();
        societyService.createSociety(society, new AbResponseListener<Society>(new TypeReference<Result<Society>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();

            }

            @Override
            public void responseInfo(Result<Society> data) {
                dialog.dismiss();
                ToastUtil.showMessage("新建圈子成功!");
                society.attach.joinnum = 1l;
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("team", data.getData());
                intent.putExtras(bundle);
                EventBus.getDefault().post(new SocietyEvent(society, SocietyEvent.CODE_ADD));
                setResult(RESULT_OK, intent);
                finish();

            }
        });

    }


    @Override
    public void initListener() {

    }
}
